package com.basic.modules.ykf.controller;

import java.util.Arrays;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.common.exception.RRException;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.common.validator.ValidatorUtils;

import com.basic.modules.ykf.entity.YkfDictDetailEntity;
import com.basic.modules.ykf.service.YkfDictDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.basic.modules.ykf.entity.YkfDictEntity;
import com.basic.modules.ykf.service.YkfDictService;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.R;



/**
 * 字典
 *
 * @author maoheng
 * @email 15738859597@163.com
 * @date 2024-03-20 09:22:08
 */
@RestController
@RequestMapping("ykf/dict")
public class YkfDictController {

    @Autowired
    private YkfDictService ykfDictService;

    @Autowired
    private YkfDictDetailService dictDetailService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(){
        return R.ok(ykfDictService.list());
    }

    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody YkfDictEntity params){
        if (ObjectUtil.isEmpty(params) || ObjectUtil.isEmpty(params.getDictId())) {
            throw new RRException("参数缺失");
        }
        return R.ok(ykfDictService.getById(params.getDictId()));
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody YkfDictEntity ykfDict){
        ValidatorUtils.validateEntity(ykfDict);
        QueryWrapper<YkfDictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(YkfDictEntity::getDictNo, ykfDict.getDictNo());
        if (ykfDictService.count(queryWrapper) > 0) {
            throw new RRException("字典编号已存在，请检查");
        }
		ykfDictService.save(ykfDict);
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody YkfDictEntity ykfDict){
        ValidatorUtils.validateEntity(ykfDict);
        QueryWrapper<YkfDictEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(YkfDictEntity::getDictNo, ykfDict.getDictNo())
                .ne(YkfDictEntity::getDictId, ykfDict.getDictId());
        if (ykfDictService.count(queryWrapper) > 0) {
            throw new RRException("字典编号已存在，请检查");
        }
		ykfDictService.updateById(ykfDict);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] dictIds){
		ykfDictService.removeByIds(Arrays.asList(dictIds));
        QueryWrapper<YkfDictDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(YkfDictDetailEntity::getDictId, Arrays.asList(dictIds));
        dictDetailService.remove(queryWrapper);
        return R.ok();
    }

}
